Exercises
Controlled Country Picker
The “Big List O’ Countries” select is a staple of e-commerce sites. Let's build one!
Using the data provided in the COUNTRIES
object, create a <select>
tag with options for every country. Bind this <select>
tag to the provided country
state variable.
Acceptance Criteria:
- Use the
COUNTRIES
constant to dynamically generate the set of<option>
elements.- In order to map over an object, you'll need to use something like Object.keys() or Object.entries()
- There should be a "blank" option, selected by default. It shouldn't default to the first country in the list.
- The indicator at the bottom should update when the user changes their selected country.
- No warnings in the dev console
Code Playground
Solution:
Two-Factor Authentication
Two-factor authentication has quickly become a best practice in terms of securing logins for highly-sensitive accounts. The most common form I've seen is that the user is prompted to enter a short code, generated from an app.
In this exercise, we'll build this form!
Acceptance Criteria:
- The input's value should be held in React state.
- When the user submits their code, a
window.alert
should let them know whether it's correct or not, by comparing their submitted value with theCORRECT_CODE
constant. - A
<form>
tag should be used.
Code Playground
Solution:
Generative Art
In this exercise, we're building a tool to produce generative art!
The tool is nearly complete, but the form controls need to be wired up. Your job is to connect the React state to the form controls, so that tweaking the controls will update the art.
Acceptance Criteria:
- The range slider should be bound to the
numOfLines
state. - The select control should be bound to the
colorTheme
state. - The radio buttons should be bound to the
shape
state. - The radio button labels should work correctly. The user should be able to click the text "Polygons" to select that option.
- The inputs should conform to HTML standards (eg. radio buttons should be grouped using the “name” attribute).
Note: All of your changes should happen in App.js
. The other files are shown in case you're curious how it works, but you can safely ignore them and focus exclusively on App.js
.
Code Playground
Solution:
Video Summary
- We can wire up our
numOfLines
slider as if it was a text input, settingvalue
andonChange
- Similarly, the
colorMode
select can be wired up by passingvalue
andonChange
to the parent<select>
tag (not touching the options) - The radio buttons require a bit of fixing, before we can get to the React state stuff:
- We need to add a
name
so that the two radio buttons function as a group, so that only one can be selected at a time - We need to add an
id
and anhtmlFor
so that the label functions properly
- To wire them up, we need to add a
value
prop, but unlike with<select>
and<input>
,value
isn't the React state-binding attribute. Instead,value
specifies what each option should hold.- Think of it like how the
value
property works on<option>
inside a<select>
.
- To start binding to React state, we need to add a
checked
property to each one. This property is a boolean value, depending on whether the current option should be selected or not. - Finally, we add an
onChange
handler. This works very similar to the other inputs we've seen. - Don't neglect the “value” prop! While it can be skipped by specifying a string in the
onChange
handler, this is not recommended, as it deviates from DOM specifications, and affects the ability for this form to work without React.